package com.ruoyi.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.common.Result;
import com.ruoyi.business.entity.CouponInfo;
import com.ruoyi.business.service.FrontCouponInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ao
 * @since 2024-01-08
 */
@RestController
@RequestMapping("/business/couponInfo")
public class FrontCouponInfoController {

    @Autowired
    private FrontCouponInfoService frontCouponInfoService;


    //获取优惠券列表
    @ApiOperation(value = "获取优惠券列表")
    @GetMapping("list")
    private Result<List<CouponInfo>> getList(){
        List<CouponInfo> list = frontCouponInfoService.list();
        return Result.success(list);
    }

    @GetMapping("/selectCouponInfo")
    public Result<List<CouponInfo>> selectCouponInfo(){
        List<CouponInfo> list = frontCouponInfoService.list();
        // 获取当前系统时间
        LocalDateTime currentTime = LocalDateTime.now();

        for (CouponInfo couponInfo : list) {
            if (couponInfo.getExpireTime() != null && currentTime.isAfter(couponInfo.getExpireTime())) {
                // 优惠券已经过期
                couponInfo.setIsExpired(1); // 标记优惠券为过期状态
                // 在这里可以更新数据库中的优惠券信息，将is_expired字段设置为1表示过期
            } else {
                // 优惠券未过期
                couponInfo.setIsExpired(0); // 标记优惠券为未过期状态
                // 在这里可以更新数据库中的优惠券信息，将is_expired字段设置为0表示未过期
            }
        }

        frontCouponInfoService.updateBatchById(list);


        List<CouponInfo> result = list.stream()
                .filter(item -> item.getIsExpired() == 0)
                .collect(Collectors.toList());

        return Result.success(result);
    }

}

